#pragma warning(disable:4200)

#include "QGui.h"
#include "QWidgets.h"
#include "awtk.h"

bitmap_t getBitmap(const QImage &image)
{    
    bitmap_t bitmap;
    image_manager_t* imm = image_manager();
    image_manager_get_bitmap(imm, image.id().c_str(), &bitmap);
    return bitmap;
}

//QPen
QPen::QPen()
{
}

QPen::QPen(Qt::GlobalColor c)
{
    m_color = QColor(c);
}

QColor QPen::color() const
{
    return m_color;
}

//QBrush
QBrush::QBrush()
{
}

QBrush::QBrush(Qt::GlobalColor c)
{
    m_color = QColor(c);
}

QColor QBrush::color() const
{
    return m_color;
}

//QImage
QImage::QImage()
{
    m_width = m_height = 0;
}

QImage::QImage(QString url)
{
    load(url);
}

QString QImage::id() const
{
    return m_id;
}

bool QImage::load(QString path)
{
    m_id = path;
    bitmap_t bitmap = getBitmap(*this);
    m_width = bitmap.w;
    m_height = bitmap.h;
    return true;
}

QSize QImage::size() const
{
    return QSize(m_width, m_height);
}

int QImage::width() const 
{
    return m_width;
}

int QImage::height() const
{
    return m_height;
}

//QIcon
QIcon::QIcon()
{

}

QIcon::QIcon(QString res)
{
    m_id = res;
}

QString QIcon::id() const
{
    return m_id;
}

//QFont
QFont::QFont()
{
}

QString QFont::family() const
{
    return m_family;
}

void QFont::setFamily(QString family)
{
    m_family = family;    
}

int QFont::pixelSize() const
{
    return m_size;
}

void QFont::setPixelSize(int size)
{
    m_size = size;
}

//QPainter
QPainter::QPainter(QWidget *widget) 
{
    m_widget = widget;      
    m_rotate = 0;
    setPen(QPen(Qt::black));    
    save();
}

QPainter::~QPainter()
{
    restore();
    Q_ASSERT(m_canvasList.size() == 0);
}

canvas_t *QPainter::canvas()
{
    return m_widget->canvas();
}

QFont QPainter::font() 
{
    return QFont();
}

void QPainter::setWindow(QRect rc)
{
    
}

QRect QPainter::window()
{
    return QRect();
}

void QPainter::save()
{
    canvas_t *c = m_widget->canvas();
    CanvasInfo info;
    info.x = c->ox;
    info.y = c->oy;
    m_canvasList.push_back(info);
}

void QPainter::restore()
{
    canvas_t *c = m_widget->canvas();
    CanvasInfo info = m_canvasList.back();
    m_canvasList.pop_back();
    c->ox = info.x;
    c->oy = info.y;
}

void QPainter::setPen(QPen pen) 
{
    m_pen = pen;

    canvas_t *c = m_widget->canvas();
    _color_t color;
    color.color = pen.color().rgba();
    canvas_set_stroke_color(c, color);
}

void QPainter::setBrush(QBrush brush) 
{
    m_brush = brush;

    canvas_t *c = m_widget->canvas();
    _color_t color;
    color.color = brush.color().rgba();
    canvas_set_fill_color(c, color);
}

void QPainter::setFont(QFont ft) 
{
    canvas_t *c = m_widget->canvas();
    canvas_set_font(c, ft.family().c_str(), ft.pixelSize());
}

void QPainter::rotate(double angle)
{
    m_rotate += angle;
}

void QPainter::translate(int x, int y)
{
    canvas_t *c = m_widget->canvas();
    canvas_translate(c, x, y);
}

void QPainter::translate(QPoint pt)
{
    translate(pt.x(), pt.y());    
}

void QPainter::drawRect(QRect rc)
{
    canvas_t *c = m_widget->canvas();    
    canvas_stroke_rect(c, rc.x(), rc.x(), rc.width(), rc.height());
}

void QPainter::drawText(int x, int y, QString text)
{
    canvas_t *c = m_widget->canvas();
    _color_t color;
    color.color = m_pen.color().rgba();
    canvas_set_text_color(c, color);

    canvas_draw_utf8(c, text.c_str(), x,y);
}

void QPainter::drawText(QRect rc, int option, QString text) 
{
    canvas_t *c = m_widget->canvas();
    _color_t color;
    color.color = m_pen.color().rgba();
    canvas_set_text_color(c, color);

    align_v_t old_v = c->text_align_v;
    align_h_t old_h = c->text_align_h;    

    int align_v = ALIGN_V_NONE, align_h = ALIGN_H_NONE;
    QStyle::getAlign(option, align_v, align_h);
    if(align_v != ALIGN_V_NONE)
        c->text_align_v = (align_v_t)align_v;
    if(align_h != ALIGN_H_NONE)
        c->text_align_h = (align_h_t)align_h;

    rect_t dst = rect_init(rc.x(), rc.y(), rc.width(), rc.height());
    canvas_draw_utf8_in_rect(c, text.c_str(), &dst);
    c->text_align_v = old_v;
    c->text_align_h = old_h;
}

void QPainter::drawImage(int x, int y, const QImage &image) 
{    
    canvas_t *c = m_widget->canvas();

    bitmap_t bitmap = getBitmap(image);
    widget_t *widget = m_widget->wid();
    if (qFuzzyIsNull(m_rotate))
    {
        rect_t dst = rect_init(x, y, bitmap.w, bitmap.h);
        rect_t src = rect_init(0, 0, bitmap.w, bitmap.h);
        canvas_draw_image(c, &bitmap, &src, &dst);
    }
    else
    {
        matrix_t m;
        matrix_init(&m);
        matrix_translate(&m, c->ox, c->oy);
        matrix_rotate(&m, m_rotate / 180.0 * 3.1415926);
        matrix_translate(&m, x, y);
        canvas_draw_image_matrix(c, &bitmap, &m);
    }
}

void QPainter::drawImage(QPoint pt, const QImage &image)
{    
    drawImage(pt.x(), pt.y(), image);
}